package api

import (
	"RedWork/dao"
	"RedWork/gdao"
	"RedWork/model"
	"log"

	//"RedWork/type/reponse"
	//"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type Standard struct {
	Status bool   `json:"status"`
	Data   string `json:"data"`
}

func HomeGetAllGoods() gin.HandlerFunc {
	return func(c *gin.Context) {
		page := c.Param("page")
		var err error
		atoi := -1
		if page != "0" {
			atoi, err = strconv.Atoi(page)
			if err != nil {
				fmt.Println("strconv.Atoi", err)
			}
			atoi = atoi*10 - 10
		}

		fmt.Println("strconv.Itoa(atoi)=", strconv.Itoa(atoi))
		datas := dao.QueryAllGoods(atoi)
		//res := Standard{true,  datas}
		amap := map[string]interface{}{
			"status": true,
			"data":   datas,
		}
		c.JSON(200, amap)
	}
}

func SearchGoods() gin.HandlerFunc {
	return func(c *gin.Context) {
		status := true
		var datas interface{}
		goodname := c.Query("good_name")
		sort := c.Query("sort")

		atoi, err := strconv.Atoi(sort)
		if err != nil && sort != "" {
			log.Println(err)
			c.JSON(200, gin.H{
				"status": "false",
				"data":   "sort code is wrong",
			})
			return
		}
		if sort == "" {
			atoi = 0
		}
		if goodname == "" {
			status = false
		} else {
			datas = gdao.QueryAKindGood(goodname, atoi)
		}
		amap := map[string]interface{}{
			"status": status,
			"data":   datas,
		}
		c.JSON(200, amap)
	}
}

func GetCategoryGoods() gin.HandlerFunc {
	return func(c *gin.Context) {
		status := true
		var datas interface{}
		code := c.Query("code")
		if code == "" {
			status = false
			datas = "请求商品code为空"
		} else {
			codeint, err := strconv.Atoi(code)
			if err != nil {
				fmt.Println(err)
			}
			datas = dao.QueryCategoryGoods(codeint)
		}
		amap := map[string]interface{}{
			"status": status,
			"data":   datas,
		}
		c.JSON(200, amap)
	}
}

func GetCategory() gin.HandlerFunc {
	return func(c *gin.Context) {
		var status bool
		status = true
		amap := map[string]interface{}{
			"status": status,
			"data":   model.Category,
		}
		c.JSON(200, amap)
	}
}
